package cmd

import (
	"encoding/json"
	"fmt"
	"mig2kwdb/common"
	"mig2kwdb/controller"
	"os"

	"github.com/spf13/cobra"
)

var vfile string
var verifyCmd = &cobra.Command{
	Use:   "verify",
	Short: "Verify data between one database and KWDB",
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println("config file:", vfile)
		fileContent, err := os.ReadFile(vfile)
		if err != nil {
			fmt.Println("Error reading file:", err)
			return
		}
		var task common.MigrateTask
		err = json.Unmarshal(fileContent, &task)
		if err != nil {
			fmt.Println(err)
			return
		}
		//check the database match
		if len(task.Source.Databases) != len(task.Target.Databases) {
			fmt.Println("Source database count not match target database count")
			return
		}

		c := controller.NewController(nil, nil)
		var stats common.TaskStattistics
		c.DoVerifyTask(cmd.Context(), &task, &stats)
		if !common.IsStopped(cmd.Context()) {
			common.PrintStatistics(&stats)
		}
	},
}

func init() {
	verifyCmd.Flags().StringVarP(&vfile, "file", "f", "", "配置文件")
}
